<?php

namespace app\api\controller;

use app\admin\model\Comment;
use app\common\model\course\CourseUser;
use app\common\model\course\CourseUserComment;
use ba\Date;
use Throwable;
use ba\Captcha;
use ba\Random;
use app\common\model\User;
use think\facade\Validate;
use app\common\facade\Token;
use app\common\model\UserScoreLog;
use app\common\model\UserMoneyLog;
use app\common\controller\Frontend;
use app\api\validate\Account as AccountValidate;

class Course extends Frontend
{
    protected array $noNeedLogin = ['recommend','lists','details'];


    public function initialize(): void
    {
        parent::initialize();
    }

    public function recommend()
    {
        $params   = $this->request->param();
        $res = \app\common\model\course\Course::where(['recommend' => 1])->order('id DESC')->paginate($params['limit'] ?? 10);

        foreach ($res->items() as &$item) {
            $item->cover = full_url($item->cover);
            $item->video = full_url($item->video);
        }

        $this->success('',['lists' => $res->items(),'total' => $res->total()]);
    }

    public function lists(): void
    {
        $params = $this->request->param();
        $where = [];
        if (isset($params['cate_id'])) {
            $where[] = ['course_cate_id', '=', $params['cate_id']];
        }
        if (isset($params['keyword'])) {
            $where[] = ['title', 'like', '%'.$params['keyword'].'%'];
        }
        $res = \app\common\model\course\Course::where($where)->order('id DESC')->paginate($params['limit'] ?? 10);

        foreach ($res->items() as &$item) {
            $item->cover = full_url($item->cover);
            $item->video = full_url($item->video);
        }

        $this->success('',['lists' => $res->items(),'total' => $res->total()]);
    }

    /**
     */
    public function details(): void
    {

        $params = $this->request->param();
        if (empty($params['id'])) {
            $this->error('课程丢失');
        }
        $row = \app\common\model\course\Course::with(['comments'])->find($params['id']);
        if (!$row) $this->error('课程不存在');
        $row->video = full_url($row->video);
        $row->cover = full_url($row->cover);
        $row->save(['watch' => $row->watch + 1]);

        $vists = CourseUser::where(['user_id' => $this->auth->id, 'course_id' => $row->id, 'course_cate_id' => $row->course_cate_id])->find();
        if (!$vists) {
            CourseUser::create([
                'user_id' => $this->auth->id,
                'course_id' => $row->id,
                'course_cate_id' => $row->course_cate_id,
            ]);
        }

        $this->success('',['row' => $row]);
    }

    public function cateLog()
    {
        $params = $this->request->param();
        if (empty($params['id'])) {
            $this->error('课程丢失');
        }
        $row = \app\common\model\course\Course::find($params['id']);
        if (!$row) $this->success('',[]);

        $lists = \app\common\model\course\Course::where(['catelog_id' => $row->catelog_id])->order('id asc')->select();
        $this->success('success',['lists' => $lists]);
    }

    public function comments()
    {
        $params = $this->request->param();
        if (empty($params['course_id']) || empty($params['comment_id'])) {
            $this->error('评论错误');
        }
        $comment = Comment::find($params['comment_id']);
        if (!$comment) $this->error('评论不存在');
        $userInfo = $this->auth->getUserInfo();
        $result = CourseUserComment::create([
            'course_id' => $params['course_id'],
            'comment_id' => $params['comment_id'],
            'comment' => $comment->title,
            'user_id' => $this->auth->id,
            'avatar' => $userInfo['avatar'],
            'nickname' => $userInfo['nickname'],
            'create_time' => time()
        ]);

        $this->success('评论成功',['result' => $result]);
    }

    public function courseComment()
    {
        $params = $this->request->param();
        if (empty($params['course_id'])) {
            $this->error('课程不存在');
        }

        $res = CourseUserComment::paginate($params['limit'] ?? 10);

        $this->success('success',['lists' => $res->items(),'total' => $res->total()]);

    }

}
